package com.wl.jetpackdemo.base.db

import android.content.Context
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

/**
 *
 * Created by wangliang on 2021/1/29.
 */
object DBManager {

    private lateinit var db: AppDatabase

    private var MIGRATION_1_2: Migration = object : Migration(1,2){
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("ALTER TABLE User ADD COLUMN `desc` TEXT NOT NULL DEFAULT ''")
        }
    }

    /**
     * 建议在APP创建时初始化，传入一次上下文，一定先初始化，否则调用db()会空
     */
    fun init(appContext: Context) {
        db = Room.databaseBuilder(
            appContext,
            AppDatabase::class.java, "database-test"
        )
            .addMigrations(MIGRATION_1_2)
            .build()
    }

    fun db(): AppDatabase {
        return db
    }


}